﻿
@{
       
        var action = UrlData[0].ToLower();
        
        Dictionary<string,string> c = new Dictionary<string,string>();
        //if custName is not empty 
        var custName = Request["custName"];
        var rBy = Request["rBy"];
        string custId = string.Empty;
        string rById = string.Empty;
        if(custName!=""){
           
            custId = UtilityManager.GetVal(Request["custId"]);
            if(custId==""){
                custId="0";
            }
        }
        
        if(rBy!=""){
            rById = UtilityManager.GetVal(Request["rById"]);
            if(rById==""){
                rById="0";
            }
        }
      
        if(custId!="0"){
            custName="";
        }
        
        c["FName"]=custName;
       // c["RefByName"]=rBy;
        c["CustomerID"]=custId;
        //c["CustomerID"]=UtilityManager.GetVal(Request["custId"]);
        c["CustomerStatus"]=UtilityManager.GetVal(Request["cStatus"]);
        c["CustomerClassification"]=UtilityManager.GetVal(Request["custClass"]);
        c["Region"]=UtilityManager.GetVal(Request["region"]);
        c["Solicitor"]=UtilityManager.GetVal(Request["solicitor"]);
        c["Campaign"]=UtilityManager.GetVal(Request["campaign"]);
        c["Country"]=UtilityManager.GetVal(Request["country"]);
        c["State"]=UtilityManager.GetVal(Request["state"]);
        c["RefBy"]=rById;
        c["CashCardNo"]=UtilityManager.GetVal(Request["ccno"]);
        c["CCStatus"]=UtilityManager.GetVal(Request["ccstatus"]);
        c["CCDelStatus"]=UtilityManager.GetVal(Request["delStatus"]);
        c["DocStatus"]=UtilityManager.GetVal(Request["docStatus"]);
        c["PartnerBank"]=UtilityManager.GetVal(Request["pb"]);
        c["DelCourier"]=UtilityManager.GetVal(Request["courier"]);
        c["DelCourier"]=UtilityManager.GetVal(Request["courier"]);
        c["CCSDateFrom"]=UtilityManager.GetVal(Request["from_ccsdate"]);
        c["CCSDateTo"]=UtilityManager.GetVal(Request["to_ccsdate"]);
        c["DelSDateFrom"]=UtilityManager.GetVal(Request["from_delsdate"]);
        c["DelSDateTo"]=UtilityManager.GetVal(Request["to_delsdate"]);
        c["DelDateFrom"]=UtilityManager.GetVal(Request["from_deldate"]);
        c["DelDateTo"]=UtilityManager.GetVal(Request["to_deldate"]);
        c["ExpDateFrom"]=UtilityManager.GetVal(Request["from_expdate"]);
        c["ExpDateTo"]=UtilityManager.GetVal(Request["to_expdate"]);
        c["AppDateFrom"]=UtilityManager.GetVal(Request["from_appdate"]);
        c["AppDateTo"]=UtilityManager.GetVal(Request["to_appdate"]);
        c["WithEmail"]=UtilityManager.GetVal(Request["email"]);
        c["WithLandLineNo"]=UtilityManager.GetVal(Request["llno"]);
        c["WithMobileNo"]=UtilityManager.GetVal(Request["mobileno"]);
        c["sort"]=UtilityManager.GetVal(Request["sort"]);
        c["sortdir"]=UtilityManager.GetVal(Request["sortdir"]);
        c["SrcRegion"]=UtilityManager.GetVal(Request["src_region"]);
        
        
        
        
     
         
        //check report type
        int reportType = Convert.ToInt32(Request["report"]);
        string reportTitle=string.Empty;
        string[] cols=null;
        switch(reportType){
            case 1:
                reportTitle = "Cash Card Master List";
                cols = new []{"FName","CustomerStatus","CashCardAppNo","CashCardStatus","CardDelStatus","DelDate","Region","Solicitor"};
                break;
            case 2:
                reportTitle = "Validation List";
                cols = new []{"Customer Name","CustomerStatus","Region","Solicitor","Campaign","Country","CurrMailState","DocumentStatus","LeadAging","AppDuration","Email","LandLineNo","MobileNo"};
                break;
            case 3:
                reportTitle="Pending Application List";
                cols = new []{"FName","CashCardAppNo","CashCardStatus","CardDelStatus","AppDuration","Region","Solicitor","Country","PartnerBank","DelCourier"};
                break;
            case 4:   
                reportTitle="Applications In-Progress";
                cols = new []{"FName","","CashCardAppNo","CashCardStatus","CardDelStatus","AppDuration","Region","Solicitor","PartnerBank","DelCourier"};
                break;
            case 5:
                reportTitle="Undelivered Cards Report";
                cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","DelStatusDate",};
                break;
            case 6:
                reportTitle="Cardholder Member Status Report";
                cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","DelStatusDate",};
                break;
                
               
                
        }
        
        
        //var ccList = CashCardManager.ExtractData(c,action);
        var reportData =  ReportManager.GetData(c,reportType);
        var recCount = reportData.Count();
        var grid = new WebGrid(reportData,canSort:true,defaultSort:"FName",columnNames:cols,rowsPerPage:5,canPage:true,ajaxUpdateContainerId: "grid",ajaxUpdateCallback: "addCheck");
       // var grid = new WebGrid(reportData,canSort:false,columnNames:cols,ajaxUpdateContainerId: "grid");
        Page.ReportData=reportData;
        /*if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
            grid.SortDirection = SortDirection.Descending;
        }*/
       
}
<script>
    $(function(){
       
       addCheck();
    });
    //maintain webgrid scroll position
     $(function(){
        $('th a, tfoot a').live('click', function(){
            $(this).attr('href', '#grid-anchor');
        });
    });
    
function genXLS(){
       
       $("#reportForm").attr("action", "/Reports/GenerateExcel/" + @reportType);
       $("#reportForm").submit();
}   

function genPDF(){
       $("#reportForm").attr("action", "/Reports/GeneratePDF/" + @reportType);
       $("#reportForm").submit();
    
}
function addCheck(){
       $("#genXLS").click(function(){
            genXLS();
        });
        
        $("#genPDF").click(function(){
            genPDF();
        });
        
        
}
</script>
<style>
    .webgrid 
{
    width: 100%;
    border: 0px;
    border-collapse: collapse;
}

/*.webgrid a
{
    text-decoration: none;
    color: #000;
}*/

.webgrid-wrapper
{
    width: 100%;
}

.webgrid-title 
{
}

.webgrid-header 
{
}

.webgrid-footer
{
    width: 500%;
}
</style>

<a name="grid-anchor"></a>
 <div id="grid" style="overflow:auto">
      <div id="buttons" align="right">
            <input type="button" id="genXLS" value="Output to Excel"/>
            <input type="button" id="genPDF" value="Output to PDF"/>
            
        </div>
    
     
    
   <fieldset>           
 
 <legend>@reportTitle</legend>
    <!-- @foreach(KeyValuePair<string,string> pair in c){
        <p>@pair.Key = @pair.Value</p>
      }-->
      
    <h2>Total Records:@recCount</h2>   
     @functions{
        static IHtmlString ShowDDate(string bdate){
                      return bdate!="01-01-1900"?new HtmlString(bdate):new HtmlString("");
                } 
         static IHtmlString ShowCustomer(string id){
                    var cust = CustomerManager.GetCustomer(id);
                    if(cust!=null){
                        return new HtmlString(cust.FName + " " + cust.LName);
                    }else{
                        return new HtmlString("");
                    }
                }
        
         static IHtmlString ShowCCStatus(string status){
                   
                  var s = RefDataManager.GetCashCardStatLkUpVal();
                  string cstat = string.Empty;
                  if(status!=null){
                      foreach(var cs in s){
                        if(cs.RefID.Equals(status)){
                            cstat=cs.RefDesc;
                        }
                    }
                 }
                    return new HtmlString(cstat);
                }
                
                static IHtmlString ShowDelStatus(string del,string date){
                    var d = RefDataManager.GetCardDelLkUpVal();
                    string cdel = string.Empty;
                    string result = string.Empty;
                    if(del!=null && date!=null){
                       var duration = UtilityManager.GetDuration(Convert.ToDateTime(date));
                  
                        foreach(var cd in d){
                            if(cd.RefID.Equals(del)){
                                cdel=cd.RefDesc;
                            }
                        }
                        if((string.Compare(cdel.ToLower(),"pending")==0) || (string.Compare(cdel.ToLower(),"delivered")==0)){
                            result = string.Format("{0}",cdel);
                        }else{
                            result = string.Format("{0}({1})",cdel,duration);
                        }
                    }
                  
                    
                    return new HtmlString(result);
                }
                
                static IHtmlString ShowDuration(string date){
                    DateTime today = DateTime.Now;
                    DateTime end = Convert.ToDateTime(date);
                    TimeSpan diff = today-end;
                    
                    return new HtmlString(diff.Days.ToString());
                }
                
                 static IHtmlString ShowMobileNo(string mobileNo){
                     return mobileNo!="63"?new HtmlString(mobileNo):new HtmlString("");
                }
                
                static IHtmlString ShowLLNo(string llNo){
                     return llNo!="63"?new HtmlString(llNo):new HtmlString("");
                }
                
                static IHtmlString GetDuration(string date){
                    TimeSpan diff = TimeSpan.Zero;
                    DateTime today = DateTime.Now;
                    if(date!=null){
                        diff = today-Convert.ToDateTime(date);
                    }
                   
                    if(diff!=TimeSpan.Zero){
                         return new HtmlString(string.Format("({0})",diff.Days.ToString()));
                    }else{
                        return new HtmlString(string.Empty);
                    }
                   
                   
                }
                
                
                static IHtmlString ShowDocStatus(string ds){
                    string docStat = UtilityManager.ShowDocStatus(ds);
                    
                    return new HtmlString(docStat);
                }
                
                
     
     }
   
       @switch(reportType){
        case 1:
      @grid.GetHtml(
               tableStyle:"webgrid",
               alternatingRowStyle:"webgrid-alternating-row",
               headerStyle:"ui-widget-header",
               fillEmptyRows:true,
                columns: grid.Columns(
                 grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/edit/@item.CustomerID">Edit</a>),
                  grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/view/@item.CustomerID">View</a>),
               /* grid.Column(
                    columnName:"FName",
                    header:"Customer",
                    format:@<text>@ShowCustomer(item.CustomerID.ToString())</text>
                ),*/
                grid.Column(
                    columnName:"FName",
                    header:"Customer",
                    format:@<text>@string.Format("{0} {1}",item.FName.ToString(),item.LName.ToString())</text>
                ),
                  grid.Column(
                    columnName:"CustomerStatus",
                    header:"Customer Status"
                 ),
                  grid.Column(
                    columnName:"CashCardAppNo",
                    header:"Cash Card No"
                 ),
                 grid.Column(
                    columnName:"CashCardStatus",
                    header:"Cash Card Status",
                    format:@<text>@ShowCCStatus((item.CashCardStatus!=null)?item.CashCardStatus.ToString():null)@GetDuration((item.CashCardStatusDate!=null)?item.CashCardStatusDate.ToString():null)</text>
                 ),
                 grid.Column(
                    columnName:"CardDelStatus",
                    header:"Delivery Status",
                    format:@<text>@ShowDelStatus((item.CardDelStatus!=null)?item.CardDelStatus.ToString():null,(item.DelStatusDate!=null)?item.DelStatusDate.ToString():null)</text>
                 ),
                 grid.Column(
                    columnName:"DelDate",
                    header:"Delivery Date",
                    format:@<text>@ShowDDate((item.DelDate!=null)?item.DelDate.ToString("MM-dd-yyyy"):null)</text>
                 ),
                 grid.Column(
                    columnName:"Region",
                    header:"Region"
                 ),
                 grid.Column(
                    columnName:"Solicitor",
                    header:"Solicitor"
                 )
                
            
              )
        
    
       )
            break;
        case 2:
              @grid.GetHtml(
               tableStyle:"webgrid",
               alternatingRowStyle:"webgrid-alternating-row",
               headerStyle:"ui-widget-header",
               fillEmptyRows:true,
                columns: grid.Columns(
                 grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/edit/@item.CustomerID">Edit</a>),
                  grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/view/@item.CustomerID">View</a>),
                grid.Column(
                    columnName:"FName",
                    header:"Customer",
                    format:@<text>@string.Format("{0} {1}",item.FName.ToString(),item.LName.ToString())</text>
                ),
                 grid.Column(
                    columnName:"Region",
                    header:"Region"
                 ),
                 grid.Column(
                    columnName:"Solicitor",
                    header:"Solicitor"
                 ),
                  grid.Column(
                    columnName:"Campaign",
                    header:"Campaign"
                 ),
                  grid.Column(
                    columnName:"Country",
                    header:"Country"
                 ),
                  grid.Column(
                    columnName:"CurrMailState",
                    header:"State"
                 ),
                  grid.Column(
                    columnName:"DocumentStatus",
                    header:"Document Status",
                    format:@<text>@ShowDocStatus((item.DocumentStatus!=null)?item.DocumentStatus.ToString():string.Empty)</text>
                 ),
                /* grid.Column(
                    columnName:"ApplicationDate",
                    header:"Duration(Application)",
                    format:@<text>@ShowDuration(item.ApplicationDate.ToString())</text>
                 ),*/
                  grid.Column(
                    columnName:"LeadAging",
                    header:"Lead Aging"
                  
                 ),
                  grid.Column(
                    columnName:"AppDuration",
                    header:"Duration(Application)"
                  
                 ),
                 grid.Column(
                    columnName:"Email",
                    header:"With Email"
                 ),
                 grid.Column(
                    columnName:"LandLineNo",
                    header:"With Landline",
                    format:@<text>@ShowLLNo(item.LandLineNo.ToString().Replace("|",""))</text>
                 ),
                 grid.Column(
                    columnName:"MobileNo",
                    header:"With Mobile",
                    format:@<text>@ShowMobileNo(item.MobileNo.ToString().Replace("|",""))</text>
                 )
                
              )
        
    
       )
            break;
        case 3:
               @grid.GetHtml(
               tableStyle:"webgrid",
               alternatingRowStyle:"webgrid-alternating-row",
               headerStyle:"ui-widget-header",
               fillEmptyRows:true,
                columns: grid.Columns(
                 grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/edit/@item.CustomerID">Edit</a>),
                  grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/view/@item.CustomerID">View</a>),
                grid.Column(
                    columnName:"FName",
                    header:"Customer",
                   format:@<text>@string.Format("{0} {1}",item.FName.ToString(),item.LName.ToString())</text>
                ),
                 grid.Column(
                    columnName:"CashCardAppNo",
                    header:"Cash Card No"
                 ),
                 grid.Column(
                    columnName:"CashCardStatus",
                    header:"Cash Card Status",
                    format:@<text>@ShowCCStatus(item.CashCardStatus.ToString())@GetDuration(item.CashCardStatusDate.ToString())</text>
                 ),
                 grid.Column(
                    columnName:"CardDelStatus",
                    header:"Delivery Status",
                    format:@<text>@ShowDelStatus(item.CardDelStatus.ToString(),item.DelStatusDate.ToString())</text>
                 ),
                 /*grid.Column(
                    columnName:"ApplicationDate",
                    header:"Duration(Application)",
                    format:@<text>@ShowDuration(item.ApplicationDate.ToString())</text>
                 ),*/
                 grid.Column(
                    columnName:"AppDuration",
                    header:"Duration(Application)"
                  
                 ),
                 grid.Column(
                    columnName:"Region",
                    header:"Region"
                 ),
                 grid.Column(
                    columnName:"Solicitor",
                    header:"Solicitor"
                 ),
                
                  grid.Column(
                    columnName:"Country",
                    header:"Country"
                 ),
                grid.Column(
                    columnName:"PartnerBank",
                    header:"Partner Bank"
                 ),
                grid.Column(
                    columnName:"CardDelCourier",
                    header:"Delivery Courier"
                 )
               
                
              )
        
    
       )
            break;
        case 4:
               @grid.GetHtml(
               tableStyle:"webgrid",
               alternatingRowStyle:"webgrid-alternating-row",
               headerStyle:"ui-widget-header",
               fillEmptyRows:true,
                columns: grid.Columns(
                 grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/edit/@item.CustomerID">Edit</a>),
                  grid.Column(
                      header:"",
                      format:@<a href="/Customer/Index/view/@item.CustomerID">View</a>),
                grid.Column(
                    columnName:"FName",
                    header:"Customer",
                    format:@<text>@string.Format("{0} {1}",item.FName.ToString(),item.LName.ToString())</text>
                ),
                 grid.Column(
                    columnName:"CashCardAppNo",
                    header:"Cash Card No"
                 ),
                 grid.Column(
                    columnName:"CashCardStatus",
                    header:"CashCard Status",
                    format:@<text>@ShowCCStatus(item.CashCardStatus.ToString())@GetDuration(item.CashCardStatusDate.ToString())</text>
                 ),
                 grid.Column(
                    columnName:"CardDelStatus",
                    header:"Delivery Status",
                    format:@<text>@ShowDelStatus(item.CardDelStatus.ToString(),item.DelStatusDate.ToString())</text>
                 ),
                 /*grid.Column(
                    columnName:"ApplicationDate",
                    header:"Duration(Application)",
                    format:@<text>@ShowDuration(item.ApplicationDate.ToString())</text>
                 ),*/
                 grid.Column(
                    columnName:"AppDuration",
                    header:"Duration(Application)"
                  
                 ),
                 grid.Column(
                    columnName:"Region",
                    header:"Region"
                 ),
                 grid.Column(
                    columnName:"Solicitor",
                    header:"Solicitor"
                 ),
                
                grid.Column(
                    columnName:"PartnerBank",
                    header:"Partner Bank"
                 ),
                grid.Column(
                    columnName:"CardDelCourier",
                    header:"Delivery Courier"
                 )
               
                
              )
        
    
       )
            break;
    }
       
      </fieldset>
   
   
   </div>
